package com.waiter.moximoxi.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.exception.RequestException;
import com.landicorp.android.eptapi.utils.QrCode;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import static com.landicorp.android.eptapi.utils.QrCode.ECLEVEL_Q;

/**
 * 针对无打印机终端需使用外接打印机，如蓝牙打印机等。该示例针对内置打印机。
 */

public abstract class PrinterImpl extends BaseDevice {
    private Printer.Progress progress;
    private List<Printer.Step> stepList;
    private Context context;

    public PrinterImpl(Context context) {
        this.context = context;
    }

    public int getPrinterStatus() {
        try {
            int status = Printer.getInstance().getStatus();
            return status;
        } catch (RequestException e) {
            e.printStackTrace();
        }
        return PrinterError.FAIL;
    }

    public void init() {
        stepList = new ArrayList<Printer.Step>();
    }

    public boolean addText(List<Object> printer_list) {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.setAutoTrunc(false);
                Printer.Format format = new Printer.Format();

                try {
                    if (printer_list != null && printer_list.size() > 0) {

                        List<String> print_one = (List<String>) printer_list.get(0);

                        format.setAscScale(Printer.Format.ASC_SC2x2);
                        format.setAscSize(Printer.Format.ASC_DOT24x12);
                        format.setHzScale(Printer.Format.HZ_SC1x2);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);
                        printer.printMid(print_one.get(0) + '\n');

                        format.setAscScale(Printer.Format.ASC_SC1x1);
                        format.setAscSize(Printer.Format.ASC_DOT24x12);
                        format.setHzScale(Printer.Format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);
                        if (!TextUtils.isEmpty(print_one.get(1))) {
                            printer.printMid(print_one.get(1) + '\n');
                        }
                        print_lines(printer, format);

//----------------------------------------------------------------------------------------------------------------------------------------
                        List<String> print_two = (List<String>) printer_list.get(1);
                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        Printer.Alignment alignment = Printer.Alignment.LEFT;
                        printer.setFormat(format);

                        for (int i = 0; i < print_two.size(); i++) {
                            if (!TextUtils.isEmpty(print_two.get(i))) {
                                printer.printText(alignment, print_two.get(i) + '\n');
                            }
                        }


                        print_lines(printer, format);
//----------------------------------------------------------------------------------------------------------------------------------------

                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);

                        List<String> print_three = (List<String>) printer_list.get(2);
                        for (int i = 0; i < print_three.size(); i++) {
                            if (!TextUtils.isEmpty(print_three.get(i))) {
                                if (i == print_three.size() - 1) {
                                    printer.printText(alignment, print_three.get(i) + '\n');
                                } else {
                                    printer.printText(alignment, print_three.get(i) + "    ");
                                }
                            }
                        }

//----------------------------------------------------------------------------------------------------------------------------------------
                        List<List<String>> print_four = (List<List<String>>) printer_list.get(3);

                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);

                        for (int i = 0; i < print_four.size(); i++) {
                            for (int j = 0; j < print_four.get(i).size(); j++) {
                                if (!TextUtils.isEmpty(print_four.get(i).get(j))) {
                                    if (j == print_four.get(i).size() - 1) {
                                        printer.printText(alignment, print_four.get(i).get(j) + '\n');
                                    } else {
                                        printer.printText(alignment, print_four.get(i).get(j) + "   ");
                                    }
                                }
                            }
                        }

                        print_lines(printer, format);
//----------------------------------------------------------------------------------------------------------------------------------------

                        List<String> print_five = (List<String>) printer_list.get(4);
                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);

                        for (int i = 0; i < print_five.size(); i++) {
                            if (!TextUtils.isEmpty(print_five.get(i))) {
                                printer.printText(alignment, print_five.get(i) + '\n');
                            }
                        }


                        print_lines(printer, format);
//----------------------------------------------------------------------------------------------------------------------------------------


                        List<String> print_six = (List<String>) printer_list.get(5);
                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        printer.setFormat(format);

                        for (int i = 0; i < print_six.size(); i++) {
                            if (!TextUtils.isEmpty(print_six.get(i))) {
                                printer.printText(alignment, print_six.get(i) + '\n');
                            }
                        }


                        print_lines(printer, format);

//----------------------------------------------------------------------------------------------------------------------------------------

                        List<String> print_seven = (List<String>) printer_list.get(6);
                        format.setAscScale(format.ASC_SC1x1);
                        format.setAscSize(format.ASC_DOT24x12);
                        format.setHzScale(format.HZ_SC1x1);
                        format.setHzSize(format.HZ_DOT24x24);
                        alignment = Printer.Alignment.CENTER;
                        printer.setFormat(format);

                        for (int i = 0; i < print_seven.size(); i++) {
                            if (!TextUtils.isEmpty(print_seven.get(i))) {
                                printer.printText(alignment, print_seven.get(i) + '\n');
                            }
                        }

                    }
                    printer.feedLine(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return true;
    }

    /**
     * 打印分割线
     *
     * @param printer
     * @param format
     * @throws Exception
     */
    private void print_lines(Printer printer, Printer.Format format) throws Exception {

        format.setAscScale(Printer.Format.ASC_SC1x1);
        format.setAscSize(Printer.Format.ASC_DOT24x12);
        format.setHzScale(Printer.Format.HZ_SC1x1);
        format.setHzSize(format.HZ_DOT24x24);
        printer.setFormat(format);

        printer.printText("--------------------------------\n");
    }

    public boolean addBitmap() {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                InputStream inputStream = context.getAssets().open("pay.bmp");
                printer.printImage(Printer.Alignment.LEFT, inputStream);
            }
        });
        return true;
    }

    public boolean addBarcode() {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.printBarCode("1234567890");
            }
        });
        return true;
    }

    public boolean addQRcode() {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.printQrCode(Printer.Alignment.CENTER,
                        new QrCode("福建联迪商用设备有限公司", ECLEVEL_Q),
                        200);
            }
        });
        return true;
    }

    public boolean feedLine(final int line) {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.feedLine(line);
            }
        });
        return true;
    }

    public boolean cutPaper() {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return false;
        }
        stepList.add(new Printer.Step() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                printer.cutPaper();
            }
        });
        return true;
    }

    public void startPrint() {
        if (stepList == null) {
            displayInfo("printer has not inited!");
            return;
        }
        progress = new Printer.Progress() {
            @Override
            public void doPrint(Printer printer) throws Exception {
                // never call
            }

            @Override
            public void onFinish(int error) {
                stepList.clear();
                if (error == Printer.ERROR_NONE) {
                    displayInfo("print success");
                } else {
                    String errorDes = getDescribe(error);
                    displayInfo("打印出错：" + errorDes);
                }
            }

            @Override
            public void onCrash() {
                stepList.clear();
                onDeviceServiceCrash();
            }
        };
        for (Printer.Step step : stepList) {
            progress.addStep(step);
        }
        try {
            progress.start();
        } catch (RequestException e) {
            e.printStackTrace();
        }
    }

    public String getDescribe(int error) {
        switch (error) {
            case Printer.ERROR_BMBLACK:
                return "黑标探测器检测到黑色信号";
            case Printer.ERROR_BUFOVERFLOW:
                return "缓冲模式下所操作的位置超出范围";
            case Printer.ERROR_BUSY:
                return "打印机处于忙状态";
            case Printer.ERROR_COMMERR:
                return "手座机状态正常，但通讯失败 (520针打特有返回值)";
            case Printer.ERROR_CUTPOSITIONERR:
                return "切纸刀不在原位 (自助热敏打印机特有返回值)";
            case Printer.ERROR_HARDERR:
                return "硬件错误";
            case Printer.ERROR_LIFTHEAD:
                return "打印头抬起 (自助热敏打印机特有返回值)";
            case Printer.ERROR_LOWTEMP:
                return "低温保护或AD出错 (自助热敏打印机特有返回值)";
            case Printer.ERROR_LOWVOL:
                return "低压保护";
            case Printer.ERROR_MOTORERR:
                return "打印机芯故障(过快或者过慢)";
            case Printer.ERROR_NOBM:
                return "没有找到黑标";
            case Printer.ERROR_NONE:
                return "正常状态，无错误";
            case Printer.ERROR_OVERHEAT:
                return "打印头过热";
            case Printer.ERROR_PAPERENDED:
                return "缺纸，不能打印";
            case Printer.ERROR_PAPERENDING:
                return "纸张将要用尽，还允许打印 (单步进针打特有返回值)";
            case Printer.ERROR_PAPERJAM:
                return "卡纸";
            case Printer.ERROR_PENOFOUND:
                return "自动定位没有找到对齐位置,纸张回到原来位置";
            case Printer.ERROR_WORKON:
                return "打印机电源处于打开状态";
            case Printer.ERROR_CUTCLEAN:
                return "纸仓堵纸，需清理纸仓";
            case Printer.ERROR_CUTERROR:
                return "切纸刀卡刀";
            case Printer.ERROR_CUTFAULT:
                return "切纸刀故障";
            case Printer.ERROR_OPENCOVER:
                return "纸仓被打开";
            default:
                return "未知错误";
        }
    }
}
